﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SurveySez.Model
{
	public partial class Game
	{
		public string Name
		{
			get { return CurrentNames.Select(n => n.Name).FirstOrDefault(); }
			set { Community.AddFact(new GameName(this, CurrentNames, value)); }
		}

		public GameSurvey AddSurvey(Survey survey)
		{
			float? max = GameSurveys.Max(gs => (float?)gs.Order);
			return Community.AddFact(new GameSurvey(this, survey, (max ?? 0.0f) + 1.0f));
		}

		public void InsertSurvey(Survey survey, GameSurvey insertBefore)
		{
			if (insertBefore == null)
				AddSurvey(survey);
			else
			{
				float? prior = GameSurveys
					.Where(gs => gs.Order < insertBefore.Order)
					.Max(gs => (float?)gs.Order);
				Community.AddFact(new GameSurvey(this, survey, ((prior ?? 0.0f) + insertBefore.Order) / 2.0f));
			}
		}

		public GameSurvey InsertSurveyAt(Survey survey, float order)
		{
			return Community.AddFact(new GameSurvey(this, survey, order));
		}
	}
}
